
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.edu.ldu.views.main;

import cn.edu.ldu.controller.UserController;

import javax.swing.*;
import java.awt.event.ActionEvent;

/**
 *
 * @author DYZ
 */
public class UserInfoUI extends javax.swing.JPanel {

    /**
     * Creates new form ChecckFriendsUI
     */
    public UserInfoUI(int width,int height) {
        initComponents();
        this.setSize(width,height);
        this.setVisible(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        btnReset = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        btnSureEdit = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtUserId = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtUsername = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtGender = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtStyle = new javax.swing.JTextArea();

        setLayout(new java.awt.BorderLayout());

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/BG.jpg"))); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 620, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        add(jPanel1, java.awt.BorderLayout.PAGE_START);

        btnReset.setText("重置信息");
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetMouseClicked(evt);
            }
        });
        btnReset.setEnabled(false);

        btnEdit.setText("修改信息");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditMouseClicked(evt);
            }
        });

        btnSureEdit.setText("确定修改");
        btnSureEdit.setVisible(false);
        btnSureEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSureEditMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(83, 83, 83)
                                .addComponent(btnReset)
                                .addGap(102, 102, 102)
                                .addComponent(btnEdit)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 95, Short.MAX_VALUE)
                                .addComponent(btnSureEdit)
                                .addGap(89, 89, 89))
        );
        jPanel2Layout.setVerticalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(btnReset)
                                        .addComponent(btnEdit)
                                        .addComponent(btnSureEdit))
                                .addContainerGap())
        );

        add(jPanel2, java.awt.BorderLayout.PAGE_END);

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("用户信息"));

        jLabel1.setText("账号：");

        txtUserId.setText(String.valueOf(UserController.getUserEntity().getId()));
        txtUserId.setEnabled(false);

        jLabel3.setText("昵称：");

        txtUsername.setText(UserController.getUserEntity().getUsername());
        txtUsername.setEnabled(false);

        jLabel4.setText("性别：");

        txtGender.setText("男");
        txtGender.setEnabled(false);

        jLabel5.setText("个性签名：");

        txtStyle.setColumns(20);
        txtStyle.setRows(5);
        txtStyle.setEnabled(false);
        jScrollPane1.setViewportView(txtStyle);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
                jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel3Layout.createSequentialGroup()
                                .addGap(49, 49, 49)
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addGroup(jPanel3Layout.createSequentialGroup()
                                                .addComponent(jLabel4)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(txtGender))
                                        .addGroup(jPanel3Layout.createSequentialGroup()
                                                .addComponent(jLabel1)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(txtUserId, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(77, 77, 77)
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(jPanel3Layout.createSequentialGroup()
                                                .addComponent(jLabel3)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGroup(jPanel3Layout.createSequentialGroup()
                                                .addComponent(jLabel5)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addContainerGap(15, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
                jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel3Layout.createSequentialGroup()
                                .addGap(29, 29, 29)
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel1)
                                        .addComponent(txtUserId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jLabel3)
                                        .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(23, 23, 23)
                                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(jLabel4)
                                                .addComponent(txtGender, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(jLabel5))
                                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addContainerGap(44, Short.MAX_VALUE))
        );

        add(jPanel3, java.awt.BorderLayout.CENTER);
    }// </editor-fold>

    /**
     * 点击修改按钮
     * 1、确定修改显示 2、重置按钮可操作 3、全部可操作
     * @param evt
     */
    private void btnEditMouseClicked(ActionEvent evt) {
        // TODO add your handling code here:
        //点击确定按钮之后，修改按钮不可按钮
        btnEdit.setEnabled(false);
        btnSureEdit.setVisible(true);
        btnReset.setEnabled(true);

        //全部可操作(除去账号外)
        txtUsername.setEnabled(true);
        txtGender.setEnabled(true);
        txtStyle.setEnabled(true);

        //确定修改显示之后，修改信息按钮不可操作
        this.setEnabled(false);
    }

    /**
     * 重置信息按钮
     * 在点击修改信息之后，才都可操作
     * @param evt
     */
    private void btnResetMouseClicked(ActionEvent evt){

        //重置出账号之外的所有信息
        txtUsername.setText(UserController.getUserEntity().getUsername());
        txtGender.setText(UserController.getUserAccountEntity().getGender().toString());
        txtGender.setText(UserController.getUserAccountEntity().getSignature());


    }

    /**
     * 确定修改按钮
     * 1、需要判定昵称是否为空
     * 2、性别 男-0 女-1 其他的不予通过
     * 3、其他的信息可以提交
     * 4、如果提交成功，弹出成功窗口；反之，弹出失败窗口
     * @param evt
     */
    private void btnSureEditMouseClicked(ActionEvent evt){

        //判断昵称是否为空
        if(txtUsername.getText().trim().equals("")){
            JOptionPane.showMessageDialog(null, "昵称不可为空", "格式输入错误", JOptionPane.ERROR_MESSAGE);
        }else{
            //判定性别是否正确格式
            if ((txtGender.getText().trim().equals("男"))||(txtGender.getText().trim().equals("女"))){
                //昵称满足条件+性别符合条件
                //选择是否继续提交
                int res = JOptionPane.showConfirmDialog(null, "是否继续提交", "是否继续", JOptionPane.YES_NO_OPTION);
                if (res == JOptionPane.YES_OPTION) {
                    // 点击“是”后执行这个代码块

                    //向服务端传数据

                    //提交成功
                    JOptionPane.showMessageDialog(null, "提交成功", "提交结果", JOptionPane.YES_OPTION);
                    btnEdit.setEnabled(true);
                    btnSureEdit.setVisible(false);
                    txtUsername.setEnabled(false);
                    txtGender.setEnabled(false);
                    txtStyle.setEnabled(false);


                    //提交失败
                } else {
                    return;
                }
            }else{
                JOptionPane.showMessageDialog(null, "请在性别里面输入：男/女", "格式输入错误", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    // Variables declaration - do not modify
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnReset;
    private javax.swing.JButton btnSureEdit;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField txtGender;
    private javax.swing.JTextArea txtStyle;
    private javax.swing.JTextField txtUserId;
    private javax.swing.JTextField txtUsername;
    // End of variables declaration
}

